import axios from 'axios'
import nim from './nim.im'
import Consts from '../assets/consts'
import Util from '../libs/util';

let instance = null

let im = {
  init: (options, handler, type = 'nim') => {
    switch (type) {
      case 'nim':
        instance = nim;
        break;

      default:
        break;
    }

    return (null != instance) ? instance.init(options, handler) : null;
  },

  deinit: _ => {
    return (null != instance) ? instance.deinit() : null
  },

  setHandler: handler => {
    if (null != instance) {
      instance.setHandler(handler)
    }
  },

  setMsgCb: cb => {
    if (null != instance) {
      instance.setMsgCb(cb)
    }
  },

  connect: _ => (null != instance) ? instance.connect() : null,
  disconnect: _ => (null != instance) ? instance.disconnect() : null,

  session: {
    pin: sid => (null != instance) ? instance.session.pin(sid) : null,
    unpin: sid => (null != instance) ? instance.session.unpin(sid) : null,

    mute: sid => (null != instance) ? instance.session.mute(sid) : null,
    unmute: sid => (null != instance) ? instance.session.unmute(sid) : null,

    read: sid => (null != instance) ? instance.session.read(sid) : null,

    list: _ => (null != instance) ? instance.session.list() : null,
    last: sid => (null != instance) ? instance.session.last(sid) : null,

    delete: (sid /*, remote = false */) =>
        (null != instance) ? instance.session.delete(sid /* , remote */) : null,
    clean: (remote = false) =>
        (null != instance) ? instance.session.clean(remote) : null,
  },

  message: {
    resend: (message) =>
        (null != instance) ? instance.message.resendMsg(message) : null,

    send: (sid, message, secret = false) =>
        (null != instance) ?
            instance.message.send(sid, message, secret) : null,

    isRead: message =>
        (null != instance) ? instance.message.isRead(message) : null,
    read: (message, deleteAfter = -1) =>
        (null != instance) ? instance.message.read(message, deleteAfter) : null,

    recall: message =>
        (null != instance) ? instance.message.recall(message) : null,

    delete: (message, remote = false) =>
        (null != instance) ? instance.message.delete(message, remote) : null,
    deleteById: (id, remote = false) =>
        (null != instance) ? instance.message.deleteById(id, remote) : null,

    forward: (sid, messages) =>
        (null != instance) ? instance.message.forward(sid, messages) : null,

    list: (sid, ts = null, limit = 50, asc = true, remote = false) =>
        (null != instance) ?
            instance.message.list(sid, ts, limit, asc, remote) : null,

    search: (sid, key) =>
        (null != instance) ? instance.message.search(sid, key) : null,

  },

  api: (url, data) =>
      new Promise((r, e) => {
        axios({
          timeout: 2000,
          method: 'post',
          withCredentials: true,

          url: Util.ajaxUrl + url,
          data: data,
          // [Neo] .TODO remove or not?
          param: data,
        }).then(res => r(res.data))
        .catch(err => e(err))
      }),

  group: {
    add: '/chat/group/add',
    delete: '/chat/group/delete',
    list: '/chat/group/list',
    query: '/chat/group/query',
    update: '/chat/group/update',
    quit: '/chat/group/quit',

    notice: {
      add: '/chat/group/notice/add',
      delete: '/chat/group/notice/delete',
      list: '/chat/group/notice/list',
      query: '/chat/group/notice/query',
    },
  },

  user: {
    info: account => (null != instance) ? instance.user.info(account) : null,
  },

  conference: {
    add: (audio = true, name, users) =>
        (null != instance) ?
            instance.conference.add(audio, name, users) : null,

    ready: sid =>
        (null != instance) ? instance.conference.ready(sid) : null,

    quit: sid =>
        (null != instance) ? instance.conference.quit(sid) : null,

    list: (page = 1, size = 10) =>
        (null != instance) ? instance.conference.list(page, size) : null,

  },
}

export default im;
